package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import easier.log.logger.Logger;
import java.net.InetSocketAddress;
import java.net.URI;
import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.handshake.ServerHandshake;
import org.java_websocket.server.WebSocketServer;

public class MainActivity83 extends AppCompatActivity {

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity83.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main83 );

        getSupportFragmentManager().beginTransaction().replace( R.id.container, new ClientFragment() ).commit();
        getSupportFragmentManager().beginTransaction().replace( R.id.container1, new ServerFragment() ).commit();
    }

    public static class ClientFragment extends Fragment {

        private final URI uri = URI.create( "ws://127.0.0.1:2048" );
        private Client client;

        private Button mConnect;
        private Button mSend;
        private Button mClose;

        @Nullable
        @Override
        public View onCreateView( @NonNull LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState ) {

            return inflater.inflate( R.layout.activity_main83_fragment, container, false );
        }

        @Override
        public void onViewCreated( @NonNull View view, @Nullable Bundle savedInstanceState ) {

            super.onViewCreated( view, savedInstanceState );

            initView( view );
            initViewAction();
        }

        private void initViewAction() {

            client = new Client( uri );

            mConnect.setOnClickListener( new OnClickListener() {
                @Override
                public void onClick( View v ) {

                    client.connect();
                }
            } );

            mSend.setOnClickListener( new OnClickListener() {
                @Override
                public void onClick( View v ) {

                    client.send( "hello server" );
                }
            } );

            mClose.setOnClickListener( new OnClickListener() {
                @Override
                public void onClick( View v ) {

                    client.close();
                }
            } );
        }

        private void initView( View view ) {

            mConnect = (Button) view.findViewById( R.id.connect );
            mSend = (Button) view.findViewById( R.id.send );
            mClose = (Button) view.findViewById( R.id.close );
        }
    }

    public static class Client extends WebSocketClient {

        public Client( URI serverUri ) {

            super( serverUri );
        }

        @Override
        public void onOpen( ServerHandshake serverHandshake ) {

            Logger.i( serverHandshake.getHttpStatusMessage() );
        }

        @Override
        public void onMessage( String s ) {

            Logger.i( s );
        }

        @Override
        public void onClose( int i, String s, boolean b ) {

            Logger.i( String.format( "%d %s %b", i, s, b ) );
        }

        @Override
        public void onError( Exception e ) {

            Logger.i( e.getMessage() );
        }
    }

    public static class ServerFragment extends Fragment {

        private Server mServer;

        private Button mConnect;
        private Button mSend;
        private Button mClose;

        @Nullable
        @Override
        public View onCreateView( @NonNull LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState ) {

            return inflater.inflate( R.layout.activity_main83_fragment, container, false );
        }

        @Override
        public void onViewCreated( @NonNull View view, @Nullable Bundle savedInstanceState ) {

            super.onViewCreated( view, savedInstanceState );

            initView( view );
            initViewAction();
        }

        private void initViewAction() {

            mServer = new Server( new InetSocketAddress( "127.0.0.1", 2048 ) );

            mConnect.setOnClickListener( new OnClickListener() {
                @Override
                public void onClick( View v ) {

                    mServer.start();
                }
            } );

            mSend.setOnClickListener( new OnClickListener() {
                @Override
                public void onClick( View v ) {

                    mServer.broadcast( "服务器发送的消息" );
                }
            } );

            mClose.setOnClickListener( new OnClickListener() {
                @Override
                public void onClick( View v ) {

                    try {
                        mServer.stop();
                    } catch ( InterruptedException e ) {
                        e.printStackTrace();
                    }
                }
            } );
        }

        private void initView( View view ) {

            mConnect = (Button) view.findViewById( R.id.connect );
            mSend = (Button) view.findViewById( R.id.send );
            mClose = (Button) view.findViewById( R.id.close );
        }
    }

    public static class Server extends WebSocketServer {

        public Server( InetSocketAddress address ) {

            super( address );
        }

        @Override
        public void onOpen( WebSocket webSocket, ClientHandshake clientHandshake ) {

            Logger.i( webSocket + " " + clientHandshake );
        }

        @Override
        public void onClose( WebSocket webSocket, int i, String s, boolean b ) {

            Logger.i( webSocket + " " + i + " " + s + " " + b );
        }

        @Override
        public void onMessage( WebSocket webSocket, String s ) {

            Logger.i( webSocket + " " + s );
        }

        @Override
        public void onError( WebSocket webSocket, Exception e ) {

            Logger.i( webSocket + " " + e );
        }

        @Override
        public void onStart() {

            Logger.i( "start" );
        }
    }
}